<include file="Public/head"/>
<body class="no-skin">
<include file="Public/header"/>
<div class="main-container" id="main-container">
    <script type="text/javascript">
        try {
            ace.settings.check('main-container', 'fixed')
        } catch (e) {
        }
    </script>

    <include file="Public/sidebar"/>
    <div class="main-content">
        <div class="main-content-inner">
            <!-- #section:basics/content.breadcrumbs -->
            <include file="Public/breadcrumbs"/>

            <!-- /section:basics/content.breadcrumbs -->
            <div class="page-content">
                <include file="Public/set"/>

                <!-- /section:settings.box -->
                <div class="row">
                    <div class="col-xs-12">
                        <!-- PAGE CONTENT BEGINS -->
                        <form id="export-form" method="post" action="{:U('export')}">
                            <table class="table table-striped table-bordered">
                                <thead>
                                <tr>
                                    <th width="48"><input class="check-all" checked="chedked" type="checkbox" value="">
                                    </th>
                                    <th width="100">表名</th>
                                    <th width="100">数据量</th>
                                    <th width="100">数据大小</th>
                                    <th width="100">创建时间</th>
                                    <th width="100">备份状态</th>
                                    <th width="100">操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <volist name="list" id="table">
                                    <tr>
                                        <td class="num">
                                            <input class="ids" checked="chedked" type="checkbox" name="tables[]"
                                                   value="{$table.name}">
                                        </td>
                                        <td>{$table.name}</td>
                                        <td>{$table.rows}</td>
                                        <td>{$table.data_length|format_bytes}</td>
                                        <td>{$table.create_time}</td>
                                        <td class="info">未备份</td>
                                        <td class="action">
                                            <a class="ajax-get no-refresh"
                                               href="{:U('Database/optimize?tables='.$table['name'])}">优化表</a>&nbsp;
                                            <a class="ajax-get no-refresh"
                                               href="{:U('Database/repair?tables='.$table['name'])}">修复表</a>
                                        </td>
                                    </tr>
                                </volist>
                                </tbody>
                            </table>
                            <div class="cf">
                                <a id="export" class="btn btn-info" href="javascript:;" autocomplete="off">立即备份</a>
                                <a id="optimize" class="btn btn-info" href="{:U('Database/optimize')}">优化表</a>
                                <a id="repair" class="btn btn-info" href="{:U('Database/repair')}">修复表</a>
                                <a class="btn btn-info" href="{:U('Database/recovery')}">数据还原</a>
                            </div>
                        </form>
                        <!-- PAGE CONTENT ENDS -->
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.page-content -->
        </div>
    </div><!-- /.main-content -->
    <include file="Public/footer"/>

</div><!-- /.main-container -->

<include file="Public/footerjs"/>
<script src="__PUBLIC__/qwadmin/js/bootbox.js"></script>
<!-- inline scripts related to this page -->
<script type="text/javascript">
    (function ($) {
        //全选的实现
        $(".check-all").click(function () {
            $(".ids").prop("checked", this.checked);
        });
        $(".ids").click(function () {
            var option = $(".ids");
            option.each(function (i) {
                if (!this.checked) {
                    $(".check-all").prop("checked", false);
                    return false;
                } else {
                    $(".check-all").prop("checked", true);
                }
            });
        });
        var $form = $("#export-form"), $export = $("#export"), tables
        $optimize = $("#optimize"), $repair = $("#repair");

        $optimize.add($repair).click(function () {
            $.post(this.href, $form.serialize(), function (data) {
                if (data.status) {
                    bootbox.dialog({
                        message: data.info,
                        buttons: {
                            "success": {
                                "label": "确定",
                                "className": "btn-sm btn-primary"
                            }
                        }
                    });
                } else {
                    bootbox.dialog({
                        message: data.info,
                        buttons: {
                            "success": {
                                "label": "确定",
                                "className": "btn-danger"
                            }
                        }
                    });
                }
                setTimeout(function () {
                    $('#top-alert').find('button').click();
                    $(this).removeClass('disabled').prop('disabled', false);
                }, 1500);
            }, "json");
            return false;
        });

        $export.click(function () {
            $export.parent().children().addClass("disabled");
            $export.html("正在发送备份请求...");
            $.post(
                    $form.attr("action"),
                    $form.serialize(),
                    function (data) {
                        if (data.status) {
                            tables = data.tables;
                            $export.html(data.info + "开始备份，请不要关闭本页面！");
                            backup(data.tab);
                            window.onbeforeunload = function () {
                                return "正在备份数据库，请不要关闭！"
                            }
                        } else {
                            alert(data.info, 'alert-error');
                            $export.parent().children().removeClass("disabled");
                            $export.html("立即备份");
                            setTimeout(function () {
                                $('#top-alert').find('button').click();
                                $(that).removeClass('disabled').prop('disabled', false);
                            }, 1500);
                        }
                    },
                    "json"
            );
            return false;
        });

        function backup(tab, status) {
            status && showmsg(tab.id, "开始备份...(0%)");
            $.get($form.attr("action"), tab, function (data) {
                if (data.status) {
                    showmsg(tab.id, data.info);

                    if (!$.isPlainObject(data.tab)) {
                        $export.parent().children().removeClass("disabled");
                        $export.html("备份完成，点击重新备份");
                        window.onbeforeunload = function () {
                            return null
                        }
                        return;
                    }
                    backup(data.tab, tab.id != data.tab.id);
                } else {
                    alert(data.info, 'alert-error');
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                    setTimeout(function () {
                        $('#top-alert').find('button').click();
                        $(that).removeClass('disabled').prop('disabled', false);
                    }, 1500);
                }
            }, "json");

        }

        function showmsg(id, msg) {
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })(jQuery);
</script>
</body>
</html>
